
<template>
  <div class="department-container">
    <div class="app-container">
      <div class="componey">
        <div class="c-left">{{ companyInfo.name }}</div>
        <div class="c-right">
          <div class="cr-left">负责人</div>
          <div class="cr-right">
            <el-dropdown>
              <span class="el-dropdown-link">
                操作<i class="el-icon-arrow-down el-icon--right" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="add('')">添加子部门</el-dropdown-item>
                <el-dropdown-item>增加部门</el-dropdown-item>
                <el-dropdown-item>删除部门</el-dropdown-item>

              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </div>
      <div class="tree">
        <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick">
          <div slot-scope="{ data:mydata }" class="componey">
            <div class="c-left">{{ mydata.name }}</div>
            <div class="c-right">
              <div class="cr-left">{{ mydata.manager }}</div>
              <div class="cr-right">
                <el-dropdown>
                  <span class="el-dropdown-link">
                    操作<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="add(mydata)">添加子部门</el-dropdown-item>
                    <el-dropdown-item @click.native="editadd(mydata)">编辑部门</el-dropdown-item>
                    <el-dropdown-item v-if="!mydata.children||mydata.children.length===0" @click.native="weidelete(mydata)">删除部门</el-dropdown-item>

                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </div>
          </div>
        </el-tree>

      </div>
    </div>
    <!-- 因为是单独盒子 所以可以与数据代码为同一级 -->
    <!-- v-if="dialogVisible"就是为了在市场部编辑取消后,在销售部编辑是就不去会在框子里面显示市场部的数据 -->
    <el-dialog

      v-if="dialogVisible"
      :title="isEdit?'编辑':'新增' "
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
    >
      <UpdateDlog :arrlist-tochild="arrlistTochild" :is-edit="isEdit" :current-pid="currentPid" @ShanChu="dialogVisible=false" @chenggong="shixian" />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartmentjia } from '@/api/department'
import { DELETEdata } from '@/api/editXiugai'

import { transformListToTree2 } from '@/utils'
import UpdateDlog from '@/views/departments/UpdateDlog.vue'
export default {
  components: {
    UpdateDlog
  },
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      companyInfo: {},
      dialogVisible: false,
      currentPid: '',
      // / 当前的模态框是编辑还是新增
      isEdit: false,
      arrlistTochild: []
    }
  },
  async created() {
    this.qinQIU()
  },
  methods: {
    async qinQIU() {
      // try {
      const res = await getDepartmentjia()
      console.log('22222222222222222', res)
      // this.data = res.data.depts
      this.arrlistTochild = res.data.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name,
          code: item.code

        }
      })

      this.data = transformListToTree2(res.data.depts, '')

      // 数组第一条数据.shift()
      this.companyInfo = res.data.depts.shift()
      // } catch (err) {
      //   console.log('错误')
      // }
    },
    handleNodeClick(data) {
      // console.log(data)
    },
    add(param) {
      this.isEdit = false
      console.log(111111111111, param)
      if (param) {
        // 二级路由的意思
        this.currentPid = param.id
      } else {
        this.currentPid = ''
      }
      this.dialogVisible = true
    },
    editadd(param) {
      this.isEdit = true
      this.dialogVisible = true
      this.currentPid = param.id
      console.log('编辑打印', param)
    },
    shixian() {
      this.qinQIU()
      this.dialogVisible = false
    },
    async shanchu(id) {
      await DELETEdata(id)
      this.qinQIU()
    },
    weidelete(param) {
      console.log(param.id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.shanchu(param.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss">
.app-container{
 .componey{
  display: flex;
  width: 100%;
  .c-left{
    flex: 1 1 80%;
    padding:5px 10px;
    // background-color:aquamarine;
  }
.c-right{
   flex: 1 1 20%;
    padding:5px 10px;
    // background-color:rgb(51, 18, 237);
    display:flex;
    .cr-left{
      flex: 1 1 50%;
      // background-color: pink;
    }
    .cr-right{
      flex: 1 1 50%;
      // background-color: rgb(35, 217, 53);
    }
}
 }
}
</style>
